package com.dubito.palette

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.palette.graphics.Palette
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.google.android.material.tabs.TabLayout
import kotlin.math.floor


/**
 * @author by liliang on 2020/6/14 3:39 PM
 */
class TabLayoutActivity : AppCompatActivity() {

    private lateinit var tabLayout: TabLayout
    private lateinit var viewPager: ViewPager
    private lateinit var toolbar: Toolbar

    private lateinit var tab1: TabFragment
    private lateinit var tab2: TabFragment
    private lateinit var tab3: TabFragment
    private lateinit var tab4: TabFragment

    private var isInit = false

    private val fragments = mutableListOf<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.viewpager)
        toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        tabLayout = findViewById(R.id.tabLayout)
        viewPager = findViewById(R.id.viewPager)

        initFragments()

        viewPager.adapter = PagerAdapter(supportFragmentManager)
        tabLayout.setupWithViewPager(viewPager)

        addPageChangeListener()


    }

    private fun initFragments() {
        tab1 = TabFragment()
        val bundle = Bundle()
        bundle.putInt("TAB", 1)
        tab1.arguments = bundle
        tab2 = TabFragment()
        val bundle2 = Bundle()
        bundle2.putInt("TAB", 2)
        tab2.arguments = bundle2
        tab3 = TabFragment()
        val bundle3 = Bundle()
        bundle3.putInt("TAB", 3)
        tab3.arguments = bundle3
        tab4 = TabFragment()
        val bundle4 = Bundle()
        bundle4.putInt("TAB", 4)
        tab4.arguments = bundle4
        fragments.add(tab1)
        fragments.add(tab2)
        fragments.add(tab3)
        fragments.add(tab4)
    }

    private fun addPageChangeListener() {
        viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                // 这个方法在setAdapter之后会调用一次，在这里初始化第一个界面，而且只调用一次
                if (!isInit) {
                    isInit = true
                    setPaletteColor(position)
                }
            }

            override fun onPageSelected(position: Int) {
                setPaletteColor(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
    }

    private fun setPaletteColor(position: Int) {
        var bitmap: Bitmap? = null

        //状态栏改变颜色。
        when (position) {
            0 -> {
                bitmap = tab1.getBitmap()
            }
            1 -> {
                bitmap = tab2.getBitmap()
            }
            2 -> {
                bitmap = tab3.getBitmap()
            }
            3 -> {
                bitmap = tab4.getBitmap()
            }
        }
        if (bitmap == null) {
            return
        }
        Palette.from(bitmap).generate { palette ->
            var vibrant = palette!!.vibrantSwatch
            if (vibrant == null) {
                for (swatch in palette.swatches) {
                    vibrant = swatch
                    break
                }
            }
            // 这样获取的颜色可以进行改变。
            val rbg = vibrant!!.rgb
            when (position) {
                0 -> {
                    tab1.setContent(rbg)
                }
                1 -> {
                    tab2.setContent(rbg)
                }
                2 -> {
                    tab3.setContent(rbg)
                }
                3 -> {
                    tab4.setContent(rbg)
                }
            }
            //状态栏改变颜色。
            tabLayout.setBackgroundColor(rbg)
            toolbar.setBackgroundColor(rbg)

            val window = window
            //状态栏改变颜色。
            val color = changeColor(rbg)
            window.statusBarColor = color

        }
    }


    private fun changeColor(rgb: Int): Int {
        var red = rgb shr 16 and 0xFF
        var green = rgb shr 8 and 0xFF
        var blue = rgb and 0xFF
        red = floor(red * (1 - 0.2)).toInt()
        green = floor(green * (1 - 0.2)).toInt()
        blue = floor(blue * (1 - 0.2)).toInt()
        return Color.rgb(red, green, blue)
    }

    inner class PagerAdapter(fragmentManager: FragmentManager) :
        FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

        override fun getItem(position: Int) = fragments[position]

        override fun getCount() = fragments.size

        override fun getPageTitle(position: Int) = "第${position + 1}页"

    }
}